<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加学生</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/Sys/css/layui.css">
    <link rel="stylesheet" href="/Sys/css/sign.css">
    <link rel="stylesheet" href="/Sys/css/font.css">
    <link rel="stylesheet" href="/Sys/css/xadmin.css">
    <script type="text/javascript" src="/Sys/js/jquery-3.3.1.min.js"></script>
    <script src="/Sys/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/Sys/js/xadmin.js"></script>
</head>

<body>
<div class="layui-container" style="padding: 20px;">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">教师号</label>
            <div class="layui-input-block">
                <input type="text" name="username" required lay-verify="required" placeholder="请输入教师号"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入姓名"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="phone" required lay-verify="required|phone" placeholder="请输入手机号"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" required placeholder="请输入邮箱"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">头像</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="test1">
                        <i class="layui-icon">&#xe67c;</i>选择头像
                    </button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="demo1" style="width:100px;height:100px;border-radius:50%;">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSubmit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</div>

<script>
    var imgpath;
    layui.use(['form', 'layer', 'upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            upload = layui.upload;

        // 图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: 'uploadImg'
            , field: "photo"  //默认是file
            , before: function (obj) {
                // 清除错误提示
                $('#demoText').html('');
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result);
                });
            }
            , done: function (res) {
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                imgpath = res.data;
                layer.msg('上传成功');
            }
            , error: function () {
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        // 页面加载时显示默认头像
        $('#demo1').attr('src', '/Sys/images/defaultUser.png');

        //监听提交
        form.on('submit(formSubmit)', function (data) {
            data.field.role = 'TEACHER';
            data.field.photo = imgpath; // 添加头像路径

            $.ajax({
                url: '/Sys/addUser',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function (res) {
                    if (res === "success") {
                        layer.msg('添加成功', {
                            icon: 1,
                            time: 2000
                        }, function(){
                            // 关闭当前frame
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            // 刷新父页面
                            parent.location.reload();
                        });
                    } else {
                        layer.msg('添加失败，请重试', {
                            icon: 2,
                            time: 2000
                        });
                    }
                },
                error: function () {
                    layer.msg('服务器错误，请联系管理员', {
                        icon: 2,
                        time: 2000
                    });
                }
            });
            return false;
        });
    });
</script>
</body>
</html>